package com.firefighting.maintenancer;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.firefighting.util.JdbcUtils;
import com.firefighting.util.MYJSONObject;

import net.sf.json.JSONArray;

/**
 * Servlet implementation class EditOnExecuteTask
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/EditOnExecuteTask" })
public class EditOnExecuteTask extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		System.out.println("-----"+format.format(new Date())+"    "+this.getServletName()+"-----");
		
		String userId=request.getParameter("userId");
		String task_id = request.getParameter("task_id");
		String maintenanceplan_id = request.getParameter("maintenanceplan_id");
		String second_project_id = request.getParameter("second_project_id");
		
		PrintWriter out = response.getWriter();
		MYJSONObject result_jb = new MYJSONObject();
		
		
		Connection connection = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			connection = JdbcUtils.getConnection();
			String sql = "select mtc.serial,mtc.test_name,mtc.test_content,mtc.Id from task t,maintenance_test_content mtc,maintenanceplan_content mc "
					+ " where t.id=? and t.app_id_account=? and t.maintenanceplan_id=?  and mtc.Father_maintenance_id=? "
					+ "and  mc.maintenanceplan_id=t.maintenanceplan_id and mc.maintenance_content_id=mtc.Id ";
			ps = connection.prepareStatement(sql);
			ps.setString(1, task_id);
			ps.setString(2, userId);
			ps.setString(3, maintenanceplan_id);
			//ps.setString(4, "已完成");
			ps.setString(4, second_project_id);
			rs = ps.executeQuery();
			
			JSONArray task_ja=new JSONArray();
			MYJSONObject task_jb=new MYJSONObject();
			while (rs.next()) {
				task_jb.put("serial", rs.getString(1));
				task_jb.put("test_name", rs.getString(2));
				task_jb.put("test_content", rs.getString(3));
				task_jb.put("maintenance_test_content_id", rs.getString(4));
				task_ja.add(task_jb.toString());
			}
			
			ps.close();
			rs.close();
			sql = "select mtc.serial from task t,maintenance_test_content mtc,maintenanceplan_content mc "
					+ " where t.id=? and t.app_id_account=? and t.maintenanceplan_id=? and mc.state_finished=? and mtc.Father_maintenance_id=? "
					+ "and  mc.maintenanceplan_id=t.maintenanceplan_id and mc.maintenance_content_id=mtc.Id ";
			ps = connection.prepareStatement(sql);
			ps.setString(1, task_id);
			ps.setString(2, userId);
			ps.setString(3, maintenanceplan_id);
			ps.setString(4, "已完成");
			ps.setString(5, second_project_id);
			rs = ps.executeQuery();
			String string_serial="";
			while (rs.next()) {
				System.out.println(rs.getString(1));
				string_serial +=rs.getString(1)+",";
			}
			if (!"".equals(string_serial))
			 string_serial=string_serial.substring(0, string_serial.length()-1);
			System.out.println(string_serial);
			result_jb.put("status", "true");
			result_jb.put("task_item_finished", string_serial);
			result_jb.put("task_item", task_ja);
			
		} catch (SQLException e) {
			e.printStackTrace();
			result_jb.put("status", "false");
			result_jb.put("message", "服务器异常");
		} finally {
			JdbcUtils.free(rs, ps, connection);
			out.print(result_jb.toString());
			out.flush();
			out.close();
		}
	}

}
